home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 149_01 / a685eval.c < prev    next >
Text File  |  1989-01-11  |  11KB  |  453 lines

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        6805 Cross-Assembler (Portable);
  4.     FILENAME:    A685EVAL.C;
  5.     VERSION:    0.3;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6805 family microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All    assembler features are supported except
  12.             relocation linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6805;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  22.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  23.             A port to BDS C would be extremely difficult, but see
  24.             volume CUG113.  A port to Toolworks C is untried."
  25.  
  26.     AUTHORS:    William C. Colley III;
  27. */
  28.  
  29. /*
  30.               6805 Cross-Assembler in Portable C
  31.  
  32.            Copyright (c) 1985 William C. Colley, III
  33.  
  34. Revision History:
  35.  
  36. Ver    Date        Description
  37.  
  38. 0.0    SEP 1985    Adapted from version 3.2 of the portable 6801 cross-
  39.             assembler.  WCC3.
  40.  
  41. 0.1    JUL 1986    Added compilation instructions and tweaks for CI-C86,
  42.             Eco-C88, and Lattice C.  WCC3.
  43.  
  44. 0.2    JAN 1987    Fixed bug that made "FCB 0," legal syntax.  WCC3.
  45.  
  46. 0.3    AUG 1988    Fixed a bug in the command line parser that puts it
  47.             into a VERY long loop if the user types a command line
  48.             like "A685 FILE.ASM -L".  WCC3 per Alex Cameron.
  49.  
  50. This file contains the assembler's expression evaluator and lexical analyzer.
  51. The lexical analyzer chops the input character stream up into discrete tokens
  52. that are processed by the expression analyzer and the line assembler.  The
  53. expression analyzer processes the token stream into unsigned results of
  54. arithmetic expressions.
  55. */
  56.  
  57. /*  Get global goodies:  */
  58.  
  59. #include "a685.h"
  60.  
  61. /*  Get access to global mailboxes defined in A68.C:            */
  62.  
  63. extern char line[];
  64. extern int filesp, forwd, pass;
  65. extern unsigned pc;
  66. extern FILE *filestk[], *source;
  67. extern TOKEN token;
  68.  
  69. /*  Expression analysis routine.  The token stream from the lexical    */
  70. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  71. /*  unsigned value.  If an error occurs during the evaluation, the    */
  72. /*  global flag    forwd is set to indicate to the line assembler that it    */
  73. /*  should not base certain decisions on the result of the evaluation.    */
  74.  
  75. static int bad;
  76.  
  77. unsigned expr()
  78. {
  79.     SCRATCH unsigned u;
  80.     unsigned eval();
  81.  
  82.     bad = FALSE;
  83.     u = eval(START);
  84.     return bad ? 0 : u;
  85. }
  86.  
  87. static unsigned eval(pre)
  88. unsigned pre;
  89. {
  90.     register unsigned op, u, v;
  91.     TOKEN *lex();
  92.     void exp_error(), unlex();
  93.  
  94.     for (;;) {
  95.     u = op = lex() -> valu;
  96.     switch (token.attr & TYPE) {
  97.         case REG:
  98.         case IMM:    exp_error('S');  break;
  99.  
  100.         case SEP:    if (pre != START) unlex();
  101.         case EOL:    exp_error('E');  return;
  102.  
  103.         case OPR:    if (!(token.attr & UNARY)) { exp_error('E');  break; }
  104.             u = (op == '*' ? pc :
  105.                 eval((op == '+' || op == '-') ?
  106.                 (unsigned) UOP1 : token.attr & PREC));
  107.             switch (op) {
  108.                 case '-':    u = word(-u);  break;
  109.  
  110.                 case NOT:    u ^= 0xffff;  break;
  111.  
  112.                 case HIGH:    u = high(u);  break;
  113.  
  114.                 case LOW:    u = low(u);  break;
  115.             }
  116.  
  117.         case VAL:    
  118.         case STR:    for (;;) {
  119.                 op = lex() -> valu;
  120.                 switch (token.attr & TYPE) {
  121.                 case REG:
  122.                 case IMM:   exp_error('S');  break;
  123.  
  124.                 case SEP:   if (pre != START) unlex();
  125.                 case EOL:   if (pre == LPREN) exp_error('(');
  126.                         return u;
  127.  
  128.                 case STR:
  129.                 case VAL:   exp_error('E');  break;
  130.  
  131.                 case OPR:   if (!(token.attr & BINARY)) {
  132.                         exp_error('E');  break;
  133.                         }
  134.                         if ((token.attr & PREC) >= pre) {
  135.                         unlex();  return u;
  136.                         }
  137.                         if (op != ')')
  138.                         v = eval(token.attr & PREC);
  139.                         switch (op) {
  140.                         case '+':   u += v;  break;
  141.  
  142.                         case '-':   u -= v;  break;
  143.  
  144.                         case '*':   u *= v;  break;
  145.  
  146.                         case '/':   u /= v;  break;
  147.  
  148.                         case MOD:   u %= v;  break;
  149.  
  150.                         case AND:   u &= v;  break;
  151.  
  152.                         case OR:    u |= v;  break;
  153.  
  154.                         case XOR:   u ^= v;  break;
  155.  
  156.                         case '<':   u = u < v;  break;
  157.  
  158.                         case LE:    u = u <= v;  break;
  159.  
  160.                         case '=':   u = u == v;  break;
  161.  
  162.                         case GE:    u = u >= v;  break;
  163.  
  164.                         case '>':   u = u > v;  break;
  165.  
  166.                         case NE:    u = u != v;  break;
  167.  
  168.                         case SHL:   if (v > 15)
  169.                                 exp_error('E');
  170.                                 else u <<= v;
  171.                                 break;
  172.  
  173.                         case SHR:   if (v > 15)
  174.                                 exp_error('E');
  175.                                 else u >>= v;
  176.                                 break;
  177.  
  178.                         case ')':   if (pre == LPREN)
  179.                                 return u;
  180.                                 exp_error('(');
  181.                                 break;
  182.                         }
  183.                         clamp(u);
  184.                         break;
  185.                 }
  186.             }
  187.             break;
  188.     }
  189.     }
  190. }
  191.  
  192. static void exp_error(c)
  193. char c;
  194. {
  195.     forwd = bad = TRUE;  error(c);
  196. }
  197.  
  198. /*  Lexical analyzer.  The source input character stream is chopped up    */
  199. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  200. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  201. /*  to an attribute word, a numeric value, and (possibly) a string    */
  202. /*  value.                                */
  203.  
  204. static int oldt = FALSE;
  205. static int quote = FALSE;
  206.  
  207. TOKEN *lex()
  208. {
  209.     SCRATCH char c, *p;
  210.     SCRATCH unsigned b;
  211.     SCRATCH OPCODE *o;
  212.     SCRATCH SYMBOL *s;
  213.     OPCODE *find_operator();
  214.     SYMBOL *find_symbol();
  215.     void exp_error(), make_number(), pops(), pushc(), trash();
  216.  
  217.     if (oldt) { oldt = FALSE;  return &token; }
  218.     trash();
  219.     if (isalph(c = popc())) {
  220.     pushc(c);  pops(token.sval);
  221.     if (o = find_operator(token.sval)) {
  222.         token.attr = o -> attr;
  223.         token.valu = o -> valu;
  224.     }
  225.     else {
  226.         token.attr = VAL;  token.valu = 0;
  227.         if (s = find_symbol(token.sval)) {
  228.         token.valu = s -> valu;
  229.         if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  230.         }
  231.         else exp_error('U');
  232.     }
  233.     }
  234.     else if (isnum(c)) {
  235.     pushc(c);  pops(token.sval);
  236.     for (p = token.sval; *p; ++p);
  237.     switch (toupper(*--p)) {
  238.         case 'B':    b = 2;  break;
  239.  
  240.         case 'O':
  241.         case 'Q':    b = 8;  break;
  242.  
  243.         default:    ++p;
  244.         case 'D':    b = 10;  break;
  245.  
  246.         case 'H':    b = 16;  break;
  247.     }
  248.     *p = '\0';  make_number(b);
  249.     }
  250.     else switch (c) {
  251.     case '%':   b = 2;  goto num;
  252.  
  253.     case '@':   b = 8;  goto num;
  254.  
  255.     case '$':   b = 16;
  256. num:            pops(token.sval);
  257.             make_number(b);
  258.             break;
  259.  
  260.     case '#':   token.attr = IMM;
  261.             break;
  262.  
  263.     case '(':   token.attr = UNARY + LPREN + OPR;
  264.             goto opr1;
  265.  
  266.     case ')':   token.attr = BINARY + RPREN + OPR;
  267.             goto opr1;
  268.  
  269.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;
  270.             goto opr1;
  271.  
  272.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;
  273.             goto opr1;
  274.  
  275.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;
  276.             goto opr1;
  277.  
  278.     case '/':   token.attr = BINARY + MULT + OPR;
  279. opr1:            token.valu = c;
  280.             break;
  281.  
  282.     case '<':   token.valu = c;
  283.             if ((c = popc()) == '=') token.valu = LE;
  284.             else if (c == '>') token.valu = NE;
  285.             else pushc(c);
  286.             goto opr2;
  287.  
  288.     case '=':   token.valu = c;
  289.             if ((c = popc()) == '<') token.valu = LE;
  290.             else if (c == '>') token.valu = GE;
  291.             else pushc(c);
  292.             goto opr2;
  293.  
  294.     case '>':   token.valu = c;
  295.             if ((c = popc()) == '<') token.valu = NE;
  296.             else if (c == '=') token.valu = GE;
  297.             else pushc(c);
  298. opr2:            token.attr = BINARY + RELAT + OPR;
  299.             break;
  300.  
  301.     case '\'':
  302.     case '"':   quote = TRUE;  token.attr = STR;
  303.             for (p = token.sval; (*p = popc()) != c; ++p)
  304.             if (*p == '\n') { exp_error('"');  break; }
  305.             *p = '\0';  quote = FALSE;
  306.             if ((token.valu = token.sval[0]) && token.sval[1])
  307.             token.valu = (token.valu << 8) + token.sval[1];
  308.             break;
  309.  
  310.     case ',':   token.attr = SEP;
  311.             break;
  312.  
  313.         case '\n':  token.attr = EOL;
  314.             break;
  315.     }
  316.     return &token;
  317. }
  318.  
  319. static void make_number(base)
  320. unsigned base;
  321. {
  322.     SCRATCH char *p;
  323.     SCRATCH unsigned d;
  324.     void exp_error();
  325.  
  326.     token.attr = VAL;
  327.     token.valu = 0;
  328.